package db

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"zlxBook/model"
)

var DB *gorm.DB

func InitDb() *gorm.DB {
	//dsn := "root:Wyou100kuai!@tcp(101.91.158.87:3306)/db1?charset=utf8mb4&parseTime=True&loc=Local"
	driverName := "mysql"
	password := "Wyou100kuai!"
	host := "101.91.158.87"
	database := "db1"
	username := "root"
	charset := "utf8"

	port := "3306"
	args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true", username, password, host, port, database, charset)
	db, err := gorm.Open(driverName, args)
	if err != nil {
		panic("failed to connect mysql" + err.Error())
	}
	db.AutoMigrate(&model.User{})
	DB = db
	return db
}
func Run() {
	_ = InitDb()

}
